#include "stdafx.h"
#include "GameStage.h"
#include "PFMEngine.h"
#include "RectSelect.h"
#include "RectStatic.h"
//#include "RectSelectButton.h"



GameStage::GameStage()
: m_state( STATE_TITLE )
,m_pLastSelect(NULL)
,m_cntTitleName(-30)
{}

GameStage::~GameStage()
{}

void GameStage::Init(int nStageIndex)
{
	Sound().PlayBGM("Resource/Sound/Title.wav");


	m_rectTitle = new RectStatic;
	m_rectTitle->Create(GetPFMEngine().CreateRect("Resource/Title/title.tga"), 0, 0, 800, 600);	

	for(int f=0;f<17;f++)
	{
		m_rectTitleName[f] = new RectStatic;

		char tmp[256];
		sprintf( tmp , "Resource/Title/RENDER/PushForMoney%04d.tga" , f );

		m_rectTitleName[f]->Create(GetPFMEngine().CreateRect( tmp ), 200, 50, 400, 200);	
	}


	//int x = 800 /2 - 500 / 2;
	//int y = 250;
	//int dy = 80;

	m_pStart = new RectSelectButton;
	m_pStart->Create(	GetPFMEngine().CreateRect( "Resource/Title/StartBase.tga" ), 
		GetPFMEngine().CreateRect( "Resource/Title/StartOver.tga" ), 
		GetPFMEngine().CreateRect( "Resource/Title/StartClick.tga" ),
		150, 322 , 500 , 70 );
	m_mouseCheckerTitle.AddSelectCheck(m_pStart);

	m_pCredit = new RectSelectButton;
	m_pCredit->Create(	GetPFMEngine().CreateRect( "Resource/Title/CreditBase.tga" ), 
		GetPFMEngine().CreateRect( "Resource/Title/CreditOver.tga" ), 
		GetPFMEngine().CreateRect( "Resource/Title/CreditClick.tga" ),
		150, 392 , 500 , 70 );
	m_mouseCheckerTitle.AddSelectCheck(m_pCredit);

	m_pExit = new RectSelectButton;
	m_pExit->Create(	GetPFMEngine().CreateRect( "Resource/Title/ExitBase.tga" ), 
		GetPFMEngine().CreateRect( "Resource/Title/ExitOver.tga" ), 
		GetPFMEngine().CreateRect( "Resource/Title/ExitClick.tga" ),
		150, 462 , 500 , 70 );

	m_mouseCheckerTitle.AddSelectCheck(m_pExit);


	m_pBack = new RectSelectButton;
	m_pBack->Create(	GetPFMEngine().CreateRect( "Resource/BackBase.tga" ), 
		GetPFMEngine().CreateRect( "Resource/BackOver.tga" ), 
		GetPFMEngine().CreateRect( "Resource/BackClick.tga" ),
		0, 530 , 800 , 50 );

	m_mouseCheckerTitle.AddSelectCheck(m_pBack);
	m_pBack->SetShow(FALSE);




	/////////////

	int sx = 340;
	int sy = 170;

	int x1 = 800/2 - sx  - 10;
	int y1 = 600/2 - sy  - 10 + 20;
	int x2 = 800/2 - 0   + 10;
	int y2 = 600/2 - 0   + 10 + 20;


	m_pStage1 = new RectSelectButton;
	m_pStage1->Create(	GetPFMEngine().CreateRect( "Resource/SelectStage/Stage1Base.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage1Over.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage1Click.tga" ),
						x1 , y1 , sx,sy );
	m_mouseCheckerTitle.AddSelectCheck(m_pStage1);


	m_pStage2 = new RectSelectButton;
	m_pStage2->Create(	GetPFMEngine().CreateRect( "Resource/SelectStage/Stage2Base.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage2Over.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage2Click.tga" ),
						x2 , y1 , sx,sy );
	m_mouseCheckerTitle.AddSelectCheck(m_pStage2);

	m_pStage3 = new RectSelectButton;
	m_pStage3->Create(	GetPFMEngine().CreateRect( "Resource/SelectStage/Stage3Base.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage3Over.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage3Click.tga" ),
						x1 , y2 , sx,sy );
	m_mouseCheckerTitle.AddSelectCheck(m_pStage3);

	m_pStage4 = new RectSelectButton;
	m_pStage4->Create(	GetPFMEngine().CreateRect( "Resource/SelectStage/Stage4Base.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage4Over.tga" ), 
						GetPFMEngine().CreateRect( "Resource/SelectStage/Stage4Click.tga" ),
						x2 , y2 , sx,sy );
	m_mouseCheckerTitle.AddSelectCheck(m_pStage4);


	m_pStage1->SetShow(FALSE);
	m_pStage2->SetShow(FALSE);
	m_pStage3->SetShow(FALSE);
	m_pStage4->SetShow(FALSE);

}


void GameStage::Terminate()
{

}


void GameStage::SetNextState(int next)
{
	m_InputWnd.SetNextState(next);
}

int GameStage::GetCurState()
{
	return m_InputWnd.GetCurState();
}

void GameStage::Update()
{
	switch( m_state )
	{
	case STATE_TITLE:
		{
			m_rectTitle->Update();
			m_pStart->Update();
			m_pCredit->Update();
			m_pExit->Update();

			if( m_cntTitleName > 0 && m_cntTitleName < 17 )
			{
				m_rectTitleName[m_cntTitleName]->Update();
			}
			else
			{
				m_rectTitleName[0]->Update();
			}

			m_cntTitleName++;
			if( m_cntTitleName > 60 ) m_cntTitleName = 0;
		}
		break;

	case STATE_GAME:
		{
			m_Scene.Update();
			m_InputWnd.Update();
			m_StateInfo.Update();
		}
		break;

	case  STATE_SELECTSTAGE:
		{
			m_selectStage->Update();
			m_pStage1->Update();
			m_pStage2->Update();
			m_pStage3->Update();
			m_pStage4->Update();
		}
		break;

	case  STATE_CREDIT:
		{
			m_rectCredit->Update();
		}
		break;

	case STATE_RESULT:
		{
			m_result.OnUpdate();
		}
		break;
	}

	m_pBack->Update();
}

void GameStage::Draw()
{
	GetPFMEngine().Render();
}

void GameStage::OnMouseMove(int nX, int nY)
{
	switch( m_state )
	{
	case STATE_GAME:
		{
			m_StateInfo.OnMouseMove(nX, nY);
			m_Scene.OnMouseMove(nX, nY);
			m_InputWnd.OnMouseMove(nX, nY);
		}
		break;

	case STATE_RESULT:
		{
			m_result.OnMouseMove( nX,nY );
		}
		break;

	default:
		{
			m_mouseCheckerTitle.CheckSelectRect( nX , nY );
			RectSelect* pSelect = m_mouseCheckerTitle.GetCurSelectRect();

			if( pSelect != NULL && m_pLastSelect != pSelect )
			{
				Sound().PlaySound("Resource/Sound/MenuOver.wav");
			}

			m_pLastSelect = pSelect;
		}
		break;
	}
}

void GameStage::OnMouseLDown(int nX, int nY)
{

	switch( m_state )
	{
	case STATE_GAME:
		{
			m_StateInfo.OnMouseLDown(nX, nY);
			m_Scene.OnMouseLDown(nX, nY);
			m_InputWnd.OnMouseLDown(nX, nY);
		}
		break;

	case STATE_RESULT:
		{
			m_result.OnMouseLDown( nX,nY );
		}
		break;


	default: 
		{
			m_mouseCheckerTitle.CheckSelectRect( nX , nY );
		}
		break;
	}
}

void GameStage::OnMouseLUp(int nX, int nY)
{
	m_mouseCheckerTitle.CheckSelectRect( nX , nY );

	switch( m_state )
	{
	case STATE_GAME:
		{
			m_StateInfo.OnMouseLUp(nX, nY);
			m_Scene.OnMouseLUp(nX, nY);
			m_InputWnd.OnMouseLUp(nX, nY);
		}
		break;

	case STATE_RESULT:
		{
			m_result.OnMouseLUp( nX,nY );
		}
		break;


	default:
		{
			RectSelect* pSelect = m_mouseCheckerTitle.GetCurSelectRect();

			if( pSelect != NULL &&
				pSelect != m_pStage3&&
				pSelect != m_pStage4)
			{
				Sound().PlaySound("Resource/Sound/MenuSelect.wav");
			}

			if( pSelect == m_pStart )
			{
				GoSelectStage();
			}
			else if( pSelect == m_pCredit )
			{
				GoCredit();
			}
			else if( pSelect == m_pExit )
			{
				GoExit();
			}
			else if( pSelect == m_pBack )
			{
				GoTitle();
			}
			else if( pSelect == m_pStage1 )
			{
				GoGame(0);
			}
			else if( pSelect == m_pStage2 )
			{
				GoGame(1);
			}
			else if( pSelect == m_pStage3 )
			{
				Sound().PlaySound("Resource/Sound/Error.wav");
			}
			else if( pSelect == m_pStage4 )
			{
				Sound().PlaySound("Resource/Sound/Error.wav");
			}

		}
		break;
	}

}



void GameStage::GoTitle()
{
	m_state = STATE_TITLE;

	m_pBack->SetShow(FALSE);

	m_pStart->SetShow(TRUE);
	m_pCredit->SetShow(TRUE);
	m_pExit->SetShow(TRUE);

	m_pStage1->SetShow(FALSE);
	m_pStage2->SetShow(FALSE);
	m_pStage3->SetShow(FALSE);
	m_pStage4->SetShow(FALSE);

}

void GameStage::GoCredit()
{
	m_state = STATE_CREDIT;

	m_rectCredit = new RectStatic;
	m_rectCredit->Create(GetPFMEngine().CreateRect("Resource/credit/BackGround.tga"), 0, 0, 800, 600);	

	m_pBack->SetShow(TRUE);
	m_pStart->SetShow(FALSE);
	m_pCredit->SetShow(FALSE);
	m_pExit->SetShow(FALSE);
}

void GameStage::GoSelectStage()
{
	m_state = STATE_SELECTSTAGE;

	m_selectStage = new RectStatic;
	m_selectStage ->Create(GetPFMEngine().CreateRect("Resource/SelectStage/Background.tga"), 0, 0, 800, 600);	

	m_pBack->SetShow(TRUE);
	m_pStart->SetShow(FALSE);
	m_pCredit->SetShow(FALSE);
	m_pExit->SetShow(FALSE);

	m_pStage1->SetShow(TRUE);
	m_pStage2->SetShow(TRUE);
	m_pStage3->SetShow(TRUE);
	m_pStage4->SetShow(TRUE);

}

void GameStage::GoGame(int nStageIndex)
{
	m_StateInfo.Init(nStageIndex, 0, 0);
	m_Scene.Init(nStageIndex);
	m_InputWnd.Init(nStageIndex, &m_Scene);

	m_state = STATE_GAME;

	m_pBack->SetShow(FALSE);
	m_pStart->SetShow(FALSE);
	m_pCredit->SetShow(FALSE);
	m_pExit->SetShow(FALSE);

}

void GameStage::GoExit()
{
	extern BOOL g_bMsgLoop;
	g_bMsgLoop = FALSE;
}


void GameStage::GoResult(Score &score)
{
	m_state = STATE_RESULT;

	m_result.SetScore(score);
	m_result.OnEnter();

}

